/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "NativeStorage.h"
#include "cJSON.h"
#include "FileCache.h"
#include "HttpUrl.h"
#include <bundle/native_interface_bundle.h>
#include <database/preferences/oh_preferences.h>
#include <database/preferences/oh_preferences_err_code.h>
#include <database/preferences/oh_preferences_option.h>
REGISTER_PLUGIN_CLASS(NativeStorage)

const string NativeStorage::const_file_name = "cordova_preference_db";
const string NativeStorage::const_all_key = "cordova_preference_key";
const string NativeStorage::const_all_type = "cordova_preference_type";

bool NativeStorage::execute(const string&  action, cJSON* args, CallbackContext cbc)
{
    if(action == "remove") {
        CordovaArgs cordovaArgs(args);
        string strKey = cordovaArgs.getString(0);
        if(delData(strKey,cbc)) {
            cbc.success();
        }
    }
    
    if(action == "clear") {
        if(clearData(cbc)) {
            cbc.success();
        }
    }
    
    if(action == "putBoolean") {
        CordovaArgs cordovaArgs(args);
        string strKey = cordovaArgs.getString(0);
        bool isValue = cordovaArgs.getBoolean(1);
        if(setData(strKey,&isValue, DataType_bool, cbc)) {
            cbc.success(isValue);
        }
    }
    
    if(action == "getBoolean") {
        CordovaArgs cordovaArgs(args);
        string strKey = cordovaArgs.getString(0);
        bool isValue = false;
        if(getData(strKey,&isValue, DataType_bool, cbc)) {
            cbc.success(isValue);
        }
    }
    
    if(action == "putInt") {
        CordovaArgs cordovaArgs(args);
        string strKey = cordovaArgs.getString(0);
        int nValue = cordovaArgs.getInt(1);
        if(setData(strKey,&nValue, DataType_int, cbc)) {
            cbc.success(nValue);
        }
    }
    
    if(action == "getInt") {
        CordovaArgs cordovaArgs(args);
        string strKey = cordovaArgs.getString(0);
        int nValue = 0;
        if(getData(strKey,&nValue, DataType_int, cbc)) {
            cbc.success(nValue);
        }
    }
    
    if(action == "putDouble") {
        CordovaArgs cordovaArgs(args);
        string strKey = cordovaArgs.getString(0);
        double dbValue = cordovaArgs.getDouble(1);
        if(setData(strKey,&dbValue, DataType_double, cbc)) {
            char szBuf[256];
            sprintf(szBuf, "%f", dbValue);
            cbc.success(szBuf);
        }
    }
    
    if(action == "getDouble") {
        CordovaArgs cordovaArgs(args);
        string strKey = cordovaArgs.getString(0);
        double dbValue = 0;
        if(getData(strKey,&dbValue, DataType_double, cbc)) {
            char szBuf[256];
            sprintf(szBuf, "%f", dbValue);
            cbc.success(szBuf);
        }
    }
    
    if(action == "putString" || action == "setItem" || action == "setItemWithPassword") {
        CordovaArgs cordovaArgs(args);
        string strKey = cordovaArgs.getString(0);
        string strValue = cordovaArgs.getString(1);
        if(setData(strKey,(char*)strValue.c_str(), DataType_string, cbc)) {
            cbc.success(strValue);
        }
    }
    
    if(action == "getString") {
        CordovaArgs cordovaArgs(args);
        string strKey = cordovaArgs.getString(0);
        string strValue;
        if(getData(strKey,&strValue, DataType_string, cbc)) {
            cbc.success(strValue);
        }
    }
    
    if(action == "getItem") {
        CordovaArgs cordovaArgs(args);
        string strKey = cordovaArgs.getString(0);
        string strValue;
        if(getData(strKey,&strValue, DataType_item, cbc)) {
            cbc.success(strValue);
        }
    }
    
    if(action == "getItemWithPassword") {
        CordovaArgs cordovaArgs(args);
        string strKey = cordovaArgs.getString(0);
        string strValue;
        if(getData(strKey,&strValue, DataType_string, cbc)) {
            cbc.success(strValue);
        }
    }
    
    if(action == "keys") {
        cJSON* pJsonArray = cJSON_CreateArray();
        if(getAllKey(cbc, pJsonArray)) {
            cbc.success(pJsonArray);
        }
    }
    
    return true;
}

bool NativeStorage::setData(const string & strKey, void* pArg, DataType dataType, CallbackContext cbc)
{
    // 1. 创建Preferences配置选项。
    OH_PreferencesOption *option = OH_PreferencesOption_Create();
    if (option == nullptr) {
        cbc.error("OH_PreferencesOption_Create error");
        return false;
    }
    
    // 设置Preferences配置选项的文件名称。
    int ret = OH_PreferencesOption_SetFileName(option, NativeStorage::const_file_name.c_str());
    if (ret != PREFERENCES_OK) {
        (void)OH_PreferencesOption_Destroy(option);
        cbc.error("OH_PreferencesOption_SetFileName error");
        return false;
    }
    
    
    // 设置Preferences配置选项的包名称。
    OH_NativeBundle_ApplicationInfo application = OH_NativeBundle_GetCurrentApplicationInfo();
    if((ret = OH_PreferencesOption_SetBundleName(option, application.bundleName)) != PREFERENCES_OK) {
        (void)OH_PreferencesOption_Destroy(option);
        cbc.error("OH_NativeBundle_GetCurrentApplicationInfo error");
        return false;
    }
    
    // 2. 打开一个Preferences实例。
    int errCode = PREFERENCES_OK;
    OH_Preferences *preference = OH_Preferences_Open(option, &errCode);
    // option使用完毕后可直接释放，释放后需要将指针置空。
    (void)OH_PreferencesOption_Destroy(option);
    option = nullptr;
    if (preference == nullptr || errCode != PREFERENCES_OK) {
        cbc.error("OH_Preferences_Open error");
        return false;
    }

   
    switch(dataType) {
    case DataType_bool:
        if((ret = OH_Preferences_SetBool(preference, strKey.c_str(), *(bool*)pArg)) != PREFERENCES_OK) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_SetString error");
            return false;
        }
        break;
    case DataType_int:
        if((ret = OH_Preferences_SetInt(preference, strKey.c_str(), *(int*)pArg)) != PREFERENCES_OK) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_SetInt error");
            return false;
        }
        break;
    case DataType_double:
    {
        char szBuf[256];
        sprintf(szBuf, "%f", *(double*)pArg);
        if((ret = OH_Preferences_SetString(preference, strKey.c_str(), szBuf)) != PREFERENCES_OK) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_SetString error");
            return false;
        }
        break; 
    }
    case DataType_string:
    case DataType_item:
    case DataType_ItemWithPassword:
        if((ret = OH_Preferences_SetString(preference, strKey.c_str(), (char*)pArg)) != PREFERENCES_OK) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_SetString error");
            return false;
        }
        break;
    }
    
    char *stringValue = nullptr;
    uint32_t valueLen = 0;
    string strValue;
    if((ret = OH_Preferences_GetString(preference, NativeStorage::const_all_key.c_str(), &stringValue, &valueLen)) != PREFERENCES_OK) {
        if(ret != PREFERENCES_ERROR_KEY_NOT_FOUND) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_GetString error");
            return false;
        }
    } 
    if(ret == PREFERENCES_OK) {
        strValue = stringValue;
        OH_Preferences_FreeString(stringValue);
        stringValue = nullptr;
    }
    vector<string> vecKey;
    HttpUrl::SplitString(strValue, "|", vecKey);
    
    strValue = "";
    if((ret = OH_Preferences_GetString(preference, NativeStorage::const_all_type.c_str(), &stringValue, &valueLen)) != PREFERENCES_OK) {
        if(ret != PREFERENCES_ERROR_KEY_NOT_FOUND) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_GetString error");
            return false;
        }
    }
    if(ret == PREFERENCES_OK) {
        strValue = stringValue;
        OH_Preferences_FreeString(stringValue);
        stringValue = nullptr;
    }
    vector<string> vecType;
    HttpUrl::SplitString(strValue, "|", vecType);
    
   vector<string>::iterator it = find(vecKey.begin(), vecKey.end(), strKey);
    if(it == vecKey.end()) {
        char szBuf[32];
        sprintf(szBuf, "%d", dataType);
        vecType.push_back(szBuf);
        vecKey.push_back(strKey);
        string strAllKey;
        string strAllType;
        for(int i=0;i<vecKey.size(); i++) {
            if(!strAllKey.empty()) {
                strAllKey += "|";
                strAllType += "|";
            }
            strAllKey += vecKey[i];
            strAllType += vecType[i];
        }
        if((ret = OH_Preferences_SetString(preference, NativeStorage::const_all_key.c_str(), strAllKey.c_str())) != PREFERENCES_OK){
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_SetString error");
            return false;
        }
        if((ret = OH_Preferences_SetString(preference, NativeStorage::const_all_type.c_str(), strAllType.c_str())) != PREFERENCES_OK) {
            (void)OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_SetString error");
            return false;
        }
    } else {
        int nIndex = it-vecKey.begin();
        char szBuf[32];
        sprintf(szBuf, "%d", dataType);
        vecType[nIndex] = szBuf;
        string strAllType;
        for(int i=0;i<vecKey.size(); i++) {
            if(!strAllType.empty()) {
                strAllType += "|";
            }
            strAllType += vecType[i];
        }
       if((ret = OH_Preferences_SetString(preference, NativeStorage::const_all_type.c_str(), strAllType.c_str())) != PREFERENCES_OK){
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_SetString error");
            return false;
        }
    }
    
    OH_Preferences_Close(preference);
    return true;
}

bool NativeStorage::getData(const string & strKey, void* pArg, DataType dataType, CallbackContext cbc)
{
    // 1. 创建Preferences配置选项。
    OH_PreferencesOption *option = OH_PreferencesOption_Create();
    if (option == nullptr) {
        cbc.error("OH_PreferencesOption_Create error");
        return false;
    }
    
    // 设置Preferences配置选项的文件名称。
    int ret = OH_PreferencesOption_SetFileName(option, NativeStorage::const_file_name.c_str());
    if (ret != PREFERENCES_OK) {
        OH_PreferencesOption_Destroy(option);
        cbc.error("OH_PreferencesOption_SetFileName error");
        return false;
    }
    
     // 设置Preferences配置选项的包名称。
    OH_NativeBundle_ApplicationInfo application = OH_NativeBundle_GetCurrentApplicationInfo();
    if((ret = OH_PreferencesOption_SetBundleName(option, application.bundleName)) != PREFERENCES_OK){
        OH_PreferencesOption_Destroy(option);
        cbc.error("OH_PreferencesOption_SetBundleName error");
        return false;
    }
    
    // 2. 打开一个Preferences实例。
    int errCode = PREFERENCES_OK;
    OH_Preferences *preference = OH_Preferences_Open(option, &errCode);
    // option使用完毕后可直接释放，释放后需要将指针置空。
    (void)OH_PreferencesOption_Destroy(option);
    option = nullptr;
    if (preference == nullptr || errCode != PREFERENCES_OK) {
        cbc.error("OH_Preferences_Open error");
        return false;
    }
    
    switch(dataType) {
    case DataType_bool:
        if((ret = OH_Preferences_GetBool(preference, strKey.c_str(), (bool*)pArg)) != PREFERENCES_OK) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_GetBool error");
            return false;
        }
        break;
    case DataType_int:
        if((ret = OH_Preferences_GetInt(preference, strKey.c_str(),  (int*)pArg)) != PREFERENCES_OK) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_GetInt error");
            return false;
        }
        break;
    case DataType_double:
    {
        char *stringValue = nullptr;
        uint32_t valueLen = 0;
        if((ret = OH_Preferences_GetString(preference, strKey.c_str(), &stringValue, &valueLen)) != PREFERENCES_OK) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_GetString error");
            return false;
        }
        double& dbValue = *(double*)pArg;
        string strValue;
        strValue.insert(0, stringValue, valueLen);
        dbValue = atof(strValue.c_str());
        OH_Preferences_FreeString(stringValue);
        stringValue = nullptr;
        break; 
    }
    case DataType_string:
    case DataType_item:
    case DataType_ItemWithPassword:
    {
        char *stringValue = nullptr;
        uint32_t valueLen = 0;
        if((ret = OH_Preferences_GetString(preference, strKey.c_str(), &stringValue, &valueLen)) != PREFERENCES_OK) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_GetString error");
            return false;
        }
        if(ret == PREFERENCES_OK) {
            string& strValue = *(string*)pArg;
            strValue = stringValue;
            OH_Preferences_FreeString(stringValue);
            stringValue = nullptr;
        }
        break; 
    }
    }
    
    OH_Preferences_Close(preference);
    return true;    
}

bool NativeStorage::delData(const string & strKey, CallbackContext cbc)
{
       // 1. 创建Preferences配置选项。
    OH_PreferencesOption *option = OH_PreferencesOption_Create();
    if (option == nullptr) {
        cbc.error("OH_PreferencesOption_Create error");
        return false;
    }
    
    // 设置Preferences配置选项的文件名称。
    int ret = OH_PreferencesOption_SetFileName(option, NativeStorage::const_file_name.c_str());
    if (ret != PREFERENCES_OK) {
        OH_PreferencesOption_Destroy(option);
        cbc.error("OH_PreferencesOption_SetFileName error");
        return false;
    }
    
    // 设置Preferences配置选项的包名称。
    OH_NativeBundle_ApplicationInfo application = OH_NativeBundle_GetCurrentApplicationInfo();
    if((ret = OH_PreferencesOption_SetBundleName(option, application.bundleName)) != PREFERENCES_OK){
        OH_PreferencesOption_Destroy(option);
        cbc.error("OH_PreferencesOption_SetBundleName error");
        return false;
    }
    
    // 2. 打开一个Preferences实例。
    int errCode = PREFERENCES_OK;
    OH_Preferences *preference = OH_Preferences_Open(option, &errCode);
    // option使用完毕后可直接释放，释放后需要将指针置空。
    (void)OH_PreferencesOption_Destroy(option);
    option = nullptr;
    if (preference == nullptr || errCode != PREFERENCES_OK) {
        cbc.error("OH_Preferences_Open error");
        return false;
    }

    if((ret = OH_Preferences_Delete(preference, strKey.c_str())) != PREFERENCES_OK) {
        OH_Preferences_Close(preference);
        cbc.error("OH_Preferences_Delete error");
        return false;
    }
    
    
    char *stringValue = nullptr;
    uint32_t valueLen = 0;
    
    if((ret = OH_Preferences_GetString(preference, NativeStorage::const_all_key.c_str(), &stringValue, &valueLen)) != PREFERENCES_OK){
        if(ret != PREFERENCES_ERROR_KEY_NOT_FOUND) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_GetString error");
            return false;
        }
    }
    string strValue;
    if(ret == PREFERENCES_OK) {
        strValue = stringValue;
        OH_Preferences_FreeString(stringValue);
        stringValue = nullptr;
    }
    vector<string> vecKey;
    HttpUrl::SplitString(strValue, "|", vecKey);
    
    if((ret = OH_Preferences_GetString(preference, NativeStorage::const_all_type.c_str(), &stringValue, &valueLen)) != PREFERENCES_OK){
        if(ret != PREFERENCES_ERROR_KEY_NOT_FOUND) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_GetString error");
            return false;
        }
    }
    strValue = "";
    if(ret == PREFERENCES_OK) {
        strValue = stringValue;
        OH_Preferences_FreeString(stringValue);
        stringValue = nullptr;
    }
    vector<string> vecType;
    HttpUrl::SplitString(strValue, "|", vecType);
    
   vector<string>::iterator it = find(vecKey.begin(), vecKey.end(), strKey);
    if(it != vecKey.end()) {
        int nIndex = it-vecKey.begin();
        vecKey.erase(it);
        vecType.erase(vecType.begin()+nIndex);
        string strAllKey;
        string strAllType;
        for(int i=0;i<vecKey.size(); i++) {
            if(!strAllKey.empty()) {
                strAllKey += "|";
                strAllType += "|";
            }
            strAllKey += vecKey[i];
            strAllType += vecType[i];
        }
        if((ret = OH_Preferences_SetString(preference, NativeStorage::const_all_key.c_str(), strAllKey.c_str())) != PREFERENCES_OK){
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_SetString error");
            return false;
        }
        if((ret = OH_Preferences_SetString(preference, NativeStorage::const_all_type.c_str(), strAllType.c_str())) != PREFERENCES_OK) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_SetString error");
            return false;
        }
    } 
    
    OH_Preferences_Close(preference);
    return true;    
}

bool NativeStorage::clearData(CallbackContext cbc)
{
    // 1. 创建Preferences配置选项。
    OH_PreferencesOption *option = OH_PreferencesOption_Create();
    if (option == nullptr) {
        cbc.error("OH_PreferencesOption_Create error");
        return false;
    }
    
    // 设置Preferences配置选项的文件名称。
    int ret = OH_PreferencesOption_SetFileName(option, NativeStorage::const_file_name.c_str());
    if (ret != PREFERENCES_OK) {
        OH_PreferencesOption_Destroy(option);
        cbc.error("OH_PreferencesOption_SetFileName error");
        return false;
    }
    
    // 设置Preferences配置选项的包名称。
    OH_NativeBundle_ApplicationInfo application = OH_NativeBundle_GetCurrentApplicationInfo();
    if((ret = OH_PreferencesOption_SetBundleName(option, application.bundleName)) != PREFERENCES_OK){
        OH_PreferencesOption_Destroy(option);
        cbc.error("OH_PreferencesOption_SetBundleName error");
        return false;
    }
    
    // 2. 打开一个Preferences实例。
    int errCode = PREFERENCES_OK;
    OH_Preferences *preference = OH_Preferences_Open(option, &errCode);
    // option使用完毕后可直接释放，释放后需要将指针置空。
    (void)OH_PreferencesOption_Destroy(option);
    option = nullptr;
    if (preference == nullptr || errCode != PREFERENCES_OK) {
        cbc.error("OH_Preferences_Open error");
        return false;
    }

    char *stringValue = nullptr;
    uint32_t valueLen = 0;
    if((ret = OH_Preferences_GetString(preference, NativeStorage::const_all_key.c_str(), &stringValue, &valueLen)) != PREFERENCES_OK) {
        if(ret != PREFERENCES_ERROR_KEY_NOT_FOUND) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_GetString error");
            return false;
        }
    }
    string strValue;
    if(ret == PREFERENCES_OK) {
        strValue = stringValue;
        OH_Preferences_FreeString(stringValue);
        stringValue = nullptr;
    }
    vector<string> vecKey;
    HttpUrl::SplitString(strValue, "|", vecKey);
    for(int i=0; i<vecKey.size(); i++) {
        if((ret = OH_Preferences_Delete(preference, vecKey[i].c_str())) != PREFERENCES_OK) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_Delete error");
            return false;
        }
    }
    
    if((ret = OH_Preferences_Delete(preference, NativeStorage::const_all_key.c_str())) != PREFERENCES_OK) {
        OH_Preferences_Close(preference);
        cbc.error("OH_Preferences_Delete error");
        return false;
    }
    
    if((ret = OH_Preferences_Delete(preference, NativeStorage::const_all_type.c_str())) != PREFERENCES_OK){
        OH_Preferences_Close(preference);
        cbc.error("OH_Preferences_Delete error");
        return false;
    }
    OH_Preferences_Close(preference);
    return true;    
}

bool NativeStorage::getAllKey(CallbackContext cbc, cJSON* pJsonArray)
{
    // 1. 创建Preferences配置选项。
    OH_PreferencesOption *option = OH_PreferencesOption_Create();
    if (option == nullptr) {
        cbc.error("OH_PreferencesOption_Create error");
        return false;
    }
    
    // 设置Preferences配置选项的文件名称。
    int ret = OH_PreferencesOption_SetFileName(option, NativeStorage::const_file_name.c_str());
    if (ret != PREFERENCES_OK) {
        OH_PreferencesOption_Destroy(option);
        cbc.error("OH_PreferencesOption_SetFileName error");
        return false;
    }
    
    // 设置Preferences配置选项的包名称。
    OH_NativeBundle_ApplicationInfo application = OH_NativeBundle_GetCurrentApplicationInfo();
    if((ret = OH_PreferencesOption_SetBundleName(option, application.bundleName)) != PREFERENCES_OK){
        OH_PreferencesOption_Destroy(option);
        cbc.error("OH_PreferencesOption_SetBundleName error");
        return false;
    }
    
    // 2. 打开一个Preferences实例。
    int errCode = PREFERENCES_OK;
    OH_Preferences *preference = OH_Preferences_Open(option, &errCode);
    // option使用完毕后可直接释放，释放后需要将指针置空。
    (void)OH_PreferencesOption_Destroy(option);
    option = nullptr;
    if (preference == nullptr || errCode != PREFERENCES_OK) {
        cbc.error("OH_Preferences_Open error");
        return false;
    }

    char *stringValue = nullptr;
    uint32_t valueLen = 0;
    if((ret = OH_Preferences_GetString(preference, NativeStorage::const_all_key.c_str(), &stringValue, &valueLen)) != PREFERENCES_OK) {
        if(ret != PREFERENCES_ERROR_KEY_NOT_FOUND) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_Open error");
            return false;
        }
    }
    string strValue;
    if(ret == PREFERENCES_OK) {
        strValue = stringValue;
        OH_Preferences_FreeString(stringValue);
        stringValue = nullptr;
    }
    vector<string> vecKey;
    HttpUrl::SplitString(strValue, "|", vecKey);
    
    if((ret = OH_Preferences_GetString(preference, NativeStorage::const_all_type.c_str(), &stringValue, &valueLen)) != PREFERENCES_OK){
        if(ret != PREFERENCES_ERROR_KEY_NOT_FOUND) {
            OH_Preferences_Close(preference);
            cbc.error("OH_Preferences_GetString error");
            return false;
        }
    }
    strValue = "";
    if(ret == PREFERENCES_OK) {
        strValue = stringValue;
        OH_Preferences_FreeString(stringValue);
        stringValue = nullptr;
    }
    
    vector<string> vecType;
    HttpUrl::SplitString(strValue, "|", vecType);
    
    for(int i=0; i<vecKey.size(); i++) {
        int dataType = atoi(vecType[i].c_str());
        switch(dataType) {
        case DataType_bool:
        {
            bool isValue = false;
            if((ret = OH_Preferences_GetBool(preference, vecKey[i].c_str(), &isValue)) != PREFERENCES_OK) {
                OH_Preferences_Close(preference);
                cbc.error("OH_Preferences_GetBool error");
                return false;
            }
            cJSON* pJson = cJSON_CreateObject();
            if(isValue) {
                cJSON_AddTrueToObject(pJson, vecKey[i].c_str());
            } else {
                 cJSON_AddFalseToObject(pJson, vecKey[i].c_str());
            }
            cJSON_AddItemToArray(pJsonArray, pJson);
            break;
         }
        case DataType_int:
        {
            int nValue = 0;
            if((ret = OH_Preferences_GetInt(preference, vecKey[i].c_str(), &nValue)) != PREFERENCES_OK) {
                OH_Preferences_Close(preference);
                cbc.error("OH_Preferences_GetInt error");
                return false;
            }
            cJSON* pJson = cJSON_CreateObject();
            cJSON_AddNumberToObject(pJson, vecKey[i].c_str(), nValue);
            cJSON_AddItemToArray(pJsonArray, pJson);
            break;
        }
        case DataType_double:
        {
            char *stringValue = nullptr;
            uint32_t valueLen = 0;
            if((ret = OH_Preferences_GetString(preference, vecKey[i].c_str(), &stringValue, &valueLen)) != PREFERENCES_OK) {
                OH_Preferences_Close(preference);
                cbc.error("OH_Preferences_GetString error");
                return false;
            }
            string strValue;
            strValue = stringValue;
            OH_Preferences_FreeString(stringValue);
            stringValue = nullptr;
            double dbValue = atof(strValue.c_str());
            cJSON* pJson = cJSON_CreateObject();
            cJSON_AddNumberToObject(pJson, vecKey[i].c_str(), dbValue);
            cJSON_AddItemToArray(pJsonArray, pJson);
            break; 
        }
        case DataType_string:
        case DataType_item:
        case DataType_ItemWithPassword:
        {
            char *stringValue = nullptr;
            uint32_t valueLen = 0;
            if((ret = OH_Preferences_GetString(preference, vecKey[i].c_str(), &stringValue, &valueLen)) != PREFERENCES_OK) {
                OH_Preferences_Close(preference);
                cbc.error("OH_Preferences_GetString error");
                return false;
            }
            string strValue;
            strValue = stringValue;
            OH_Preferences_FreeString(stringValue);
            stringValue = nullptr;
            cJSON* pJson = cJSON_CreateObject();
            cJSON_AddStringToObject(pJson, vecKey[i].c_str(), strValue.c_str());
            cJSON_AddItemToArray(pJsonArray, pJson);
            break; 
        }
        }
    }
    
    OH_Preferences_Close(preference);
    return true;    
}